﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Calendar;
using BillCalendar;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace BillCalendar
{
    public partial class _Default : Page
    {
        public List<CalendarTransaction> CalendarEvents { get; set; }
        public List<PayPeriod> PayPeriods { get; set; }
        public string UserName { get; set; }
        private string password { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(tb_username.Text))
            {
                UserName = tb_username.Text;
                password = tb_password.Text;
            }
            if (UserName != null)
            {
                pnl_loggedIn.Visible = true;
                pnl_login.Visible = false;
                EventFeed calFeed = pullEvents();
                CalendarEvents = calFeed.ConvertToTransactions().OrderBy(x => x.Date).ThenBy(x => x, new CalendarTransactionComparer()).ToList();
                PayPeriods = getPayPeriods(CalendarEvents);
                //gv_list.AutoGenerateColumns = false;
                gv_list.DataSource = CalendarEvents;
                gv_list.DataBind();

                gv_payperiod_list.DataSource = PayPeriods;
                gv_payperiod_list.DataBind();

                gv_normalized_payperiods.DataSource = PayPeriod.NormalizePayPeriods(PayPeriods);
                gv_normalized_payperiods.DataBind();
            }

        }

        private EventFeed pullEvents()
        {
            // Create a query and service object:
            string calendarUrl = "http://www.google.com/calendar/feeds/lee.d.dykes@gmail.com/private/full";//
            string billCalendarUrl = "https://www.google.com/calendar/feeds/p0a8opgci1qgarulj1n24708g8%40group.calendar.google.com/private-0587a629c5427c21e77f1d3bb3056ed5/full";
            EventQuery query = new EventQuery(billCalendarUrl);
            query.StartDate = DateTime.Today.AddMonths(-1);
            query.EndDate = query.StartDate.AddMonths(6);
            query.NumberToRetrieve = 300;
            query.SingleEvents = true;
            query.RecurrenceStart = query.StartTime = DateTime.Today;
            query.RecurrenceEnd = query.EndTime = query.StartDate.AddMonths(6);
            query.SortOrder = CalendarSortOrder.ascending;
            CalendarService service = new CalendarService("beyondPages-billCalendar-0.1");
            // Set your credentials:
            service.setUserCredentials(UserName, password);


            // Tell the service to query:
            EventFeed calFeed = service.Query(query);
            tb_rawFeed.Text = calFeed.Self;
            return calFeed;
        }
        public List<PayPeriod> getPayPeriods(List<CalendarTransaction> transactions)
        {
            List<PayPeriod> tempPayPeriods=new List<PayPeriod>();
            int firstPayCheck=transactions.FindIndex(x => x.TransactionType == TransactionType.Paycheck);
            for (int i = firstPayCheck; i < transactions.Count; i++)
            {
                CalendarTransaction tempTransaction = transactions[i];
                if (tempTransaction.TransactionType == TransactionType.Paycheck)
                {
                    tempPayPeriods.Add(new PayPeriod(tempTransaction));
                }
                else if(tempPayPeriods.Count>0)
                        tempPayPeriods.Last().Bills.Add(tempTransaction);

            }

            return tempPayPeriods;
        }

        protected void btn_login_Click(object sender, EventArgs e)
        {
            
        }
        
    }
    
    
    

    

}